-- NewTeamApplyView

local NewInviteListPanel = import(".NewInviteListPanel")
local NewApplyListPanel = import(".NewApplyListPanel")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.NewApplyUINode

function ClassRef:init()
	self._panelTable = {}

	self._ui["Panel/Button_close"]:addClickEventListener(function()
		executeFunc(self._onCloseView)
	end)

	self._curRightPanelIndex = 1 -- 默认选中第一项、即邀请
	for _,btnTemp in ipairs( self._ui["Panel/Panel_tab"]:getChildren() ) do
		btnTemp:getChildByName( "dot" ):setVisible( false ) -- 先不处理dot

		btnTemp:addClickEventListener(function( sender )
			local btnName = sender:getName()
			local selectedIndex = string.sub( btnName, string.len(btnName), string.len(btnName) )

			self._curRightPanelIndex = tonumber( selectedIndex )
			self:switchPanel()
		end)
	end
end

function ClassRef:show( idx )
	self:setVisible(true)

	if idx then
		self._curRightPanelIndex = idx
	end
	self:switchPanel()
end

function ClassRef:switchPanel()
	if 2 == self._curRightPanelIndex and not self._data then --  我不是队长？
		display.pushToast( L("lua_code_text_498") )
		self._curRightPanelIndex = 1
		return
	end

	for i = 1 , 2 do
		local bagBtn = self._ui["Panel/Panel_tab/selected_btn_"..i]
		bagBtn:setTouchEnabled( true )
		bagBtn:setBright( true )
		bagBtn:getChildByName( "name_1" ):setVisible( true )
		bagBtn:getChildByName( "name_2" ):setVisible( false )

		self._ui["Panel/invite"]:setVisible( false )
		self._ui["Panel/apply"]:setVisible( false )
	end
	local bagBtn = self._ui["Panel/Panel_tab/selected_btn_"..self._curRightPanelIndex]
	bagBtn:setTouchEnabled( false )
	bagBtn:setBright( false )
	bagBtn:getChildByName( "name_1" ):setVisible( false )
	bagBtn:getChildByName( "name_2" ):setVisible( true )

	executeFunc(self._onOperate, "TeamApply_switchpanel", self._curRightPanelIndex) -- 切换标签页
end

function ClassRef:getRightPanelByIndex( rightPanelIndex )
	local curPanelTemp = self._panelTable[rightPanelIndex]
	if not curPanelTemp then
		if 1 == rightPanelIndex then -- 邀请
			curPanelTemp = NewInviteListPanel.create(self._ui["Panel/invite"])
		elseif 2 == rightPanelIndex then -- 申请
			curPanelTemp = NewApplyListPanel.create(self._ui["Panel/apply"])
		end
		curPanelTemp:registerOnOperate(function(op, ...)
					executeFunc(self._onOperate, op, ...) -- 交由controller处理
				end)
		self._panelTable[rightPanelIndex] = curPanelTemp
	end
	curPanelTemp:setVisible( true )

	return curPanelTemp
end

-- 刷新"自动申请"按钮状态
function ClassRef:refreshAutoApprove(isAutoApprove, isInCrossMatch)
	assert( self._panelTable[2], "--------->>>>>>> 在申请panel未初始化的时候企图刷新'自动申请'按钮" )
	self._panelTable[2]:refreshAutoApprove( isAutoApprove, isInCrossMatch )
end

-- 邀请目标条件不满足时的动画
function ClassRef:playTipsAnimation()
	local invitePanel = self:getRightPanelByIndex( 1 )
	invitePanel:playTipsAnimation()
end

function ClassRef:onOperate(cb)
	self._onOperate = cb
end

return ClassRef
